<!DOCTYPE html>

<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="user-scalable=no, width=device-width"/>
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.structure-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.inline-png-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.inline-svg-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.external-png-1.4.4.min.css">
        <!-- Choix du thème -->
        <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css">
        <link rel="stylesheet" href="css/bootstrap/jquery.mobile.icons.min.css">
        <!-- JavaScript -->
        <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.4.4.min.js"></script>
        <script type="text/javascript" src="js/datebox/jqm-datebox.core.js"></script>
        <script type="text/javascript" src="js/datebox/jqm-datebox.mode.datebox.js"></script>
        <title></title>

    </head>
    <body>
             
        <?php
                                    
            require_once '../controller/StageManager.php';
            require_once '../model/Stage.php'; 
            include_once '../model/config.php';
              
        ?>

        <?php if ( filter_input(INPUT_GET,'idStage') ) : ?>
            <?php if ( filter_input(INPUT_GET,'action') == "read" ): ?>
                <div data-role="page" id="Stage">
                            
                    <?php 
                          
                        // Voir l'information détailée d'un stage
                        //session_start();
                        //$idUser = $_SESSION["id"];   
                        $prevlink = 'stage.php';
                        $headerText = 'Info Stage';
                        include 'includes/header.php';
                        include 'includes/footer.php';
                     ?>
                    <div id="content" data-role="main" class="ui-content">

                        <?php
                            $managerStage = new StageManager($bdd);
                            $stageInfo = $managerStage->findStage($_GET['idStage']);
                            /*echo '<label for="nameStage"><h3>Nom</h3></label>';         
                            echo '<input type="text" name="texte" data-clear-btn="true"  id="nameStage" value="'.$stageInfo->getLibelle().'" >';
                            echo '<label for="dateCreation"><h3>dateCreation</h3></label>';   
                            echo '<input name="texte" type="text" data-role="datebox" data-datebox-mode="datebox" id="dateCreation" value="'.$stageInfo->getDateCreation().'"/>' ;                        
                            echo '<label for="lienURL"><h3>New lien</h3></label>';         
                            echo '<input type="text" name="texte" data-clear-btn="true"  id="lienURL" value="newURL" >';*/
                            echo '<a href="Tache.php?idStage=' . $_GET['idStage'] . '" class="ui-btn ui-btn-e ui-corner-all" >Tâches du stage</a>';
                            echo '<a data-transition=fade id="lienURL" href="' . $stageInfo->getUrl() . '" class="ui-btn ui-btn-d ui-corner-all"> Lien du stage</a>';                          
                            echo '<a id="delButton" name="delButton" class="ui-btn ui-btn-c ui-corner-all" >Supprimer</a>';  

                        ?>
                        
                                <!-- Pop ups-->
                                <div data-role="popup" id="popupConfirmValide" data-position-to="window">
                                        <div data-role="header" >
                                            <h1> Confirm Popup</h1>
                                        </div>
                                        <div data-role="main" class="ui-content">
                                          <center><h3>Voulez vous vraiment supprimer votre stage?</h3></center>
                                          <center>
                                                <?php
                                                      echo '<a id="confirmButton" href="stage.php?idStage='.$stageInfo->getIdStage().'&action=delete" class="ui-btn ui-corner-all ui-shadow ui-btn-e ui-btn-inline ui-icon-check ui-btn-icon-left">Oui</a>';
                                                    ?>
                                                <a id="confirmButton" href="#" class="ui-btn ui-corner-all ui-shadow ui-btn-a ui-btn-inline ui-icon-back ui-btn-icon-left" data-rel="back">Non</a>
                                          </center>
                                        </div>

                                </div>
                    </div>
                                    

                </div>
            <?php else: ?>

                            <?php 
                                //Supprimer un stage dans le BDD
                                session_start();
                                $managerStage = new StageManager($bdd);
                                $managerStage->delete($_GET['idStage']);
                                //echo "<script type='text/javascript'> document.location = 'stage.php'; </script>";
                                header("Location:stage.php");
                            ?>
            <?php endif ?>
                
        <?php else: ?>
            <?php if ( filter_input(INPUT_GET,'action') == "creation" ): ?>

                    <?php if ( filter_input(INPUT_GET,'task') ): ?>
                        
                        <?php
                            //Creer ou changer un stage dans le BDD
                            session_start();
                            $stage = json_decode(filter_input(INPUT_GET,'task'));
                            $managerStage = new StageManager($bdd);
                            $creation = date("Y-m-d"); 
                            $newStage = new Stage($stage->idStage, $_SESSION["id"], 3, $creation, $stage->URL, $stage->libelle);
                            $managerStage->add($newStage);
                            //echo "<script type='text/javascript'> document.location = 'stage.php'; </script>";
                            header("Location:stage.php");
                                                    

                       ?>
                    <?php else: ?>
                        <div data-role="page" id="Stage">
                            <?php 

                                 //Remplir l'information d'un nouveau stage
                                 //session_start();
                                                         
                                 // Montrer les erreurs
                                 ini_set('display_errors', 1);
                                 ini_set('display_startup_errors', 1);
                                 error_reporting(-1);
                                 $prevlink = 'stage.php';
                                 $headerText = 'Creation un Stage';
                                 include 'includes/header.php';
                                 include 'includes/footer.php';
                            ?>
                            
                            <div id="content" data-role="content">

                                <?php
                                     echo '<label for="nameStage"><h3>Nom</h3></label>';         
                                     echo '<input type="text" name="texte" data-clear-btn="true"  id="nameStage" value="" >';      
                                     /*echo '<label for="dateCreation"><h3>dateCreation</h3></label>';   
                                     echo '<input type="text" name="texte" data-role="datebox" data-datebox-mode="datebox" id="dateCreation" value=""/>' ;*/                   
                                     echo '<label for="lienURL"><h3>URL</h3></label>';   
                                     echo '<input type="text" name="texte" data-clear-btn="true" id="lienURL" value=""/>' ;                        
                                     echo '<a id="valideCreation" name="valideCreation"  class="ui-btn ui-btn-b ui-corner-all">Valider</a>';

                                ?> 
                                              
                                <!-- Pop ups-->
                                <div data-role="popup" id="popupMissingInfo" data-position-to="window">
                                        <div data-role="header" >
                                            <h1> Warning Popup</h1>
                                        </div>
                                        <div data-role="main" class="ui-content">
                                            <center><h3>Des champs n'ont pas été remplis!!!</h3></center>
                                        </div>

                                </div>
                                
                            </div>
                        </div>
                    <?php endif ?>        
            
            <?php else: ?>
                <div data-role="page" id="Stage">
                    <?php
                        session_start();                 
                        // Montrer les erreurs
                        ini_set('display_errors', 1);
                        ini_set('display_startup_errors', 1);
                        error_reporting(-1);
                        
                        $idUser = $_SESSION["id"];
                        $prevlink = 'accueil.php';
                        $headerText = 'Liste des stages';
                        include 'includes/header.php';
                        include 'includes/footer.php';
                    ?>
                      
                    <div id="content" data-role="main" class="ui-content">
                        <?php

                        $managerStage = new StageManager($bdd);
                        $list = $managerStage->getListStages($idUser);
                        foreach ($list as $stage) {
                            $libelle = $stage->getLibelle();
                            $url = $stage->getUrl();
                            $id = $stage->getIdStage();
                            echo '<a data-transition=turn href="stage.php?idStage=' . $id . '&action=read" data-role="button">'.$libelle.'</a>';
                        }

                        ?>
                        <a id="newStage" href="stage.php?action=creation" data-transition="flow" class="ui-btn ui-btn-b ui-btn-icon-left ui-shadow ui-icon-plus ui-corner-all"> Ajouter un stage </a> 
                    </div>
                </div>
            <?php endif ?>
        <?php endif ?>
        <script>
            $(document).ready(function(){
                var isempty=0;
                var stage = {         
                    "idStage": 0,        
                    "URL": "" ,        
                    "libelle": "" 

                };
                $("#delButton").bind("tap",function(e){
                   $("#popupConfirmValide").popup("open");
                });
                
                $("#valideCreation").bind("tap", function(e) {
                     $("input[name='texte']").each(function(){                       
                         var idElm=$(this).attr("id");
                         var valElm=$(this).val();
                         if (valElm === ""){ 
                             isempty=1; 
                         }else{
                             if (idElm === "nameStage"){
                                 stage.libelle = valElm;
                             }
                             else{
                                 stage.URL = valElm;
                             }
                         }
                     }); 
                     console.log("isemty="+isempty);
                     if (isempty === 1 ){
                         $("#popupMissingInfo").popup("open");
                         isempty=0;
                     }else{
                         window.location="stage.php?action=creation&task="+JSON.stringify(stage); 
                     }
                     return false;
                 });
                 
            });
                 
        </script>
    </body>
</html>

